package com.edeweiss.community.servlet;

import com.edeweiss.community.domain.*;
import com.edeweiss.community.services.ActivityService;
import com.edeweiss.community.services.IndexService;
import com.edeweiss.community.services.NoticeService;
import com.edeweiss.community.services.servicesImp.ActivityServiceImpl;
import com.edeweiss.community.services.servicesImp.IndexServiceImpl;
import com.edeweiss.community.services.servicesImp.NoticeServiceImp;
import com.fasterxml.jackson.databind.ObjectMapper;

import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@WebServlet("/IndexServlet")
public class IndexServlet extends BaseServlet {
    private NoticeService findNotice = new NoticeServiceImp();
    private ActivityService activityService = new ActivityServiceImpl();
    private IndexService indexService = new IndexServiceImpl();

    public String findNotice(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //调用公告的servlet
        List<Notice> notices = findNotice.findNotice();
        response.setContentType("application/json;charset=utf-8");
        ObjectMapper mapper = new ObjectMapper();
        mapper.writeValue(response.getOutputStream(),notices);

        return "index.jsp";
    }

    public String findActivity(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //调用查询举报的活动
        List<Activity> activities = activityService.indexFindActivity();
        response.setContentType("application/json;charset=utf-8");
        ObjectMapper mapper = new ObjectMapper();

        mapper.writeValue(response.getOutputStream(),activities);

        return "index.jsp";
    }

    //查询荣誉墙数据
    public String findHonorWall(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        List<Community> communities = indexService.findHonorWall();
        if(communities != null && communities.size()>0){
            request.getSession().setAttribute("wall",communities);
        }else {
            request.getSession().setAttribute("msg","还没有任何社团加入到荣誉墙！");
        }
        response.sendRedirect("honorwall.jsp");
        return "";
    }
    //查询名人堂
    public String findHallOfFame(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        List<FindHof> fhs = indexService.findHallOfFame();
        if(fhs != null && fhs.size()>0){
            request.getSession().setAttribute("fhs",fhs);
        }
        response.sendRedirect("hallOfFame.jsp");
        return "";
    }
}
